package com.ssm.rfidreader;

import java.util.HashMap;
import java.util.Map;
import java.util.Observer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.module.interaction.RXTXListener;
import com.module.interaction.ReaderHelper;
import com.rfid.RFIDReaderHelper;
import com.rfid.ReaderConnector;
import com.rfid.rxobserver.RXObserver;
import com.rfid.rxobserver.bean.RXInventoryTag;
import com.ssm.dao.StockRecordQueryMapper;
import com.ssm.service.websocket.ChatMessageHandler;
import com.util.StringTool;


@Component
public class RfidReader {
	
	final ReaderConnector mConnector = new ReaderConnector();
	
	public static StockRecordQueryMapper stockRecordQueryMapper;
	
	public String host;
	
	public int port;
	
	public int type;//数据处理类型：0绑卡；1入库；2出库
	
	@Autowired
	public RfidReader(StockRecordQueryMapper stockRecordQueryMapper) {
		RfidReader.stockRecordQueryMapper = stockRecordQueryMapper;
	}

	public RfidReader(String host, int port, int type) {
		super();
		this.host = host;
		this.port = port;
		this.type = type;
	}

	ReaderHelper mReaderHelper;
	Observer mObserver = new RXObserver() {
		
		@Override
		protected void onInventoryTag(RXInventoryTag tag) {
			System.out.println("发送实时读取后的的回调："+tag.strEPC);
			if(type == Type.BIND.getCode()) {
				//发送数据到前台页面
				ChatMessageHandler.sendMessageToWeb(tag.strEPC , "client");
			} else if(type == Type.INSTOCK.getCode()){
				//调用入库方法，传入EPC
				Map<String,String> params = new HashMap<>();
				params.put("ipAddress", host);
				params.put("rfidCardNumber",tag.strEPC);
				stockRecordQueryMapper.insertMaterialByInstockRFID(params);
			} else if (type == Type.OUTSTOCK.getCode()) {
				//调用出库方法，传入EPC
				Map<String,String> params = new HashMap<>();
				params.put("ipAddress", host);
				params.put("rfidCardNumber",tag.strEPC);
				stockRecordQueryMapper.outMaterialByOutstockRFID(params);
			}
		}
		
		@Override
		protected void onInventoryTagEnd(RXInventoryTag.RXInventoryTagEnd endTag) {
//			System.out.println("inventory end:" + endTag.mTotalRead);
			((RFIDReaderHelper) mReaderHelper).realTimeInventory((byte) 0xff,(byte)0x01);
		}
		
		@Override
		protected void onExeCMDStatus(byte cmd,byte status) {
			System.out.format("CDM:%s  Execute status:%S", 
					String.format("%02X",cmd),String.format("%02x", status));
		}
		
	};
	
	
	static RXTXListener mListener = new RXTXListener() {
		@Override
		public void reciveData(byte[] btAryReceiveData) {
			System.out.println("reciveData" + StringTool.byteArrayToString(btAryReceiveData, 0, btAryReceiveData.length));
		}

		@Override
		public void sendData(byte[] btArySendData) {
			System.out.println("sendData" + StringTool.byteArrayToString(btArySendData, 0, btArySendData.length));
		}

		@Override
		public void onLostConnect() {
			
		}
		
	};

	public void start() {
		try{
			if(0 == type){
				mReaderHelper = mConnector.connectCom(host, port);
			}else{
				mReaderHelper = mConnector.connectNet(host, port);
			}
			if(mReaderHelper != null) {
				System.out.println("Connect success!");
				try {
					mReaderHelper.registerObserver(mObserver);
	//				mReaderHelper.setRXTXListener(mListener);
					Thread.sleep(500);
					((RFIDReaderHelper) mReaderHelper).realTimeInventory((byte) 0xff,(byte)0x01);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				System.out.println("Connect faild!");
				stop();
			} 
		}catch(Exception e){
			System.out.println(e);
		}
	}
	
	public void stop() {
		mReaderHelper.unRegisterObserver(mObserver);
		mConnector.disConnect();
	}
	
}
